const jwt = require('jsonwebtoken')

module.exports = (req, res, next) => {
    const authHeader = req.headers['authorization']
    if (!authHeader) {
        return res.cc({
            code: 401,
            message: '用户未登录！'
        })
    }

    const token = authHeader.split(' ')[1]
    if (!token) {
        return res.cc({
            code: 401,
            message: 'token格式错误!'
        })
    }

    try {
        const user = jwt.verify(token, '123456')
        req.user = user
        next()
    } catch (e) {
        return res.cc({
            code: 401,
            message: "token无效或已过期!!!"
        })
    }
}